package com.karst.essentials.chapter05
{
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import org.aswing.AsWingManager;
	import org.papervision3d.cameras.CameraType;
	import org.papervision3d.cameras.SpringCamera3D;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.materials.special.CompositeMaterial;
	import org.papervision3d.objects.primitives.Cone;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.PaperPlane;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
	
	internal class SpringCameraExample extends BasicView
	{
		public function SpringCameraExample()
		{
			super( AsWingManager.getStage().stageWidth, AsWingManager.getStage().stageHeight,
				true, false, CameraType.SPRING );
			createView() ;
			startRendering() ;
			addEvents() ;
		}

		private var colorMaterial : ColorMaterial = null ;
		private var wmMaterial : WireframeMaterial = null ;
		private var materials : CompositeMaterial = null ;
		private var paperPlane : PaperPlane = null ;
		private var cone : Cone = null ;
		private function createView() : void
		{
			var camera : SpringCamera3D = SpringCamera3D( camera ) ;
			
			if( colorMaterial == null )
				colorMaterial = new ColorMaterial( 0XFFFFFF) ;
			if( wmMaterial == null )
				wmMaterial = new WireframeMaterial( 0x4848448 ) ;
			if( materials == null )
			{
				materials = new CompositeMaterial() ;
				materials.addMaterial( colorMaterial ) ;
				materials.addMaterial( wmMaterial ) ;
				materials.doubleSided = true ;
			}
			
			if( paperPlane == null )
			{
				paperPlane = new PaperPlane( materials, 2 ) ;
				paperPlane.y = 150 ;
				paperPlane.z = -1000 ;
			}
			if( cone == null)
			{
				cone = new Cone( materials ) ;
				cone.x = 200 ;
				cone.y = 250 ;
				cone.z = -200 ;
			}
			
			scene.addChild( cone ) ;
			scene.addChild( paperPlane ) ;
			
			
			for(var i:uint = 0;i <  150; i++)
			{ 
				var plane:Plane = new Plane(new ColorMaterial(Math.random() * 0xFFFFFF), 50,50, 1,1);
				plane.material.doubleSided = true;
				scene.addChild(plane);
				plane.x = Math.random()*20000 - 10000;
				plane.y = Math.random()*1500 - 750;
				plane.z = Math.random()*20000 -10000;
				plane.localRotationY = Math.random() * 180 - 90;
			}
			
			
			camera.z = -2000;
			camera.focus = 20;
			camera.far = 20000;

			// 针对 弹性摄影机
			camera.mass = 20 ;
			camera.damping = 4 ;
			camera.stiffness = 1 ;
			camera.positionOffset = new Number3D( 0, 150, -500 ) ;
			camera.lookOffset = new Number3D( 0, 0, 100 ) ;
			camera.target = paperPlane ;
		}
		
		private function addEvents() : void
		{
			AsWingManager.getStage().addEventListener( KeyboardEvent.KEY_DOWN, keyDownHandler ) ;
			AsWingManager.getStage().addEventListener( KeyboardEvent.KEY_UP, keyUpHandler ) ;
		}
		
		private function removeEvents() : void
		{
			AsWingManager.getStage().removeEventListener( KeyboardEvent.KEY_DOWN, keyDownHandler ) ;
			AsWingManager.getStage().addEventListener( KeyboardEvent.KEY_UP, keyUpHandler ) ;
		}
		
		private var forward : Boolean = false ;
		private var back : Boolean = false ;
		private var left : Boolean = false ;
		private var right : Boolean = false ;
		private function keyDownHandler( $event : KeyboardEvent ) : void
		{
			switch( $event.keyCode )
			{
				case "W".charCodeAt() :
				case Keyboard.UP :
					forward = true ;
					break ;
				case "S".charCodeAt() :
				case Keyboard.DOWN :
					back = true ;
					break ;
				case "A".charCodeAt() :
				case Keyboard.LEFT :
					left = true ;
					break ;
				case "D".charCodeAt() :
				case Keyboard.RIGHT :
					right = true ;
					break ;
			}
		}
		
		private function keyUpHandler( $event : KeyboardEvent ) : void
		{
			switch( $event.keyCode )
			{
				case "W".charCodeAt() :
				case Keyboard.UP :
					forward = false ;
					break ;
				case "S".charCodeAt() :
				case Keyboard.DOWN :
					back = false ;
					break ;
				case "A".charCodeAt() :
				case Keyboard.LEFT :
					left = false ;
					break ;
				case "D".charCodeAt() :
				case Keyboard.RIGHT :
					right = false ;
					break ;
			}
		}
		
		private function moveObj() : void
		{
			if( paperPlane )
			{
				if( forward )
				{
					paperPlane.moveForward( 30 ) ;
				}
				else if( back )
				{
					paperPlane.moveBackward( 30 ) ;
				}
				else if( left )
				{
					paperPlane.localRotationY -= 2 ;
//					paperPlane.moveLeft( 30 ) ;
				}
				else if( right )
				{
					paperPlane.localRotationY += 2 ;
//					paperPlane.moveRight( 30 ) ;
				}
				
				if( cone )
					cone.lookAt( paperPlane ) ;
			}
		}

		
		override protected function onRenderTick(event:Event=null):void
		{
			super.onRenderTick( event ) ;
			moveObj() ;
		}
	}
}